<?php
/**
 * Byzantium Library
 *
 * @author David Mora <dla.mora@gmail.com>
 * @category   Byzantium
 * @package    Byzantium
 * @copyright  Creative Commons Attribution-Share Alike 3.0 United States License
 * 2009-2010 David Mora <dla.mora@gmail.com>
 * @license    http://creativecommons.org/licenses/by-sa/3.0/us/
 * @version    $Id$
 */

/**
 * Application interface implementation class
 *
 * @author David Mora <dla.mora@gmail.com>
 * @category   Byzantium
 * @package    Byzantium
 * @copyright  Creative Commons Attribution-Share Alike 3.0 United States License
 * 2009-2010 David Mora <dla.mora@gmail.com>
 * @license    http://creativecommons.org/licenses/by-sa/3.0/us/
 *
 */
class Byzantium_Application
{
    /**
     * flag if the application already ran
     * @var bool
     */
    protected $_ran = false;

    /**
     * Defines the current running environment (production, staging, etc)
     *
     * @var string
     */
    protected $_env = null;

    /**
     * Singleton instance
     *
     * Marked only as protected to allow extension of the class. To extend,
     * simply override {@link getInstance()}.
     *
     * @var Byzantium_Application
     */
    protected static $_instance = null;

    /**
     * Singleton instance
     *
     * @return Byzantium_Application
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * @return Byzantium_Log
     */
    public function getLogger()
    {
        if (!Zend_Registry::isRegistered('log')) {
            throw new Byzantium_Exception('Log Instance has not been bootstrapped');
        }

        return Zend_Registry::get('log');
    }

    /**
     * @return Array
     */
    public function getConfig()
    {
        if (!Zend_Registry::isRegistered('config')) {
            throw new Byzantium_Exception('Config Instance has not been bootstrapped');
        }

        return Zend_Registry::get('config');
    }

    /**
     * Runs the application
     *
     * @return void
     */
    public function run()
    {
        if (!$this->_ran) {
            /** Zend_Application */
            $this->_env = APPLICATION_ENV;

            // Create application, bootstrap, and run
            $application = new Zend_Application(
                APPLICATION_ENV,
                APPLICATION_PATH . '/../configs/application.ini'
            );

            $application->bootstrap()
                        ->run();
        }

        $this->_ran = true;
    }

    /**
     * Returns the environment identifier in which the app is running
     * @return string
     */
    public function getEnvironment()
    {
        return $this->_env;
    }

    /**
     * True if the app is currently running on production environment
     *
     * @return bool
     */
    public function isProductionEnv()
    {
        return $this->getEnvironment() == 'production';
    }
}
